package com.jianshuge.app.adapter;

import java.util.ArrayList;
import java.util.List;

import com.jianshuge.app.R;
import com.jianshuge.app.bean.Behavior;
import com.jianshuge.app.bean.BooklistItem;
import com.jianshuge.app.common.BitmapManager;
import com.jianshuge.app.common.StringUtils;
import com.jianshuge.app.common.UIHelper;
import com.jianshuge.app.ui.About;
import com.jianshuge.app.widget.CustomTextView;
import com.jianshuge.app.widget.LinkView;
import com.jianshuge.app.widget.CustomTextView.divText;
import com.jianshuge.app.widget.LinkView.OnLinkClickListener;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewMyTrendsAdapter extends MyBaseAdapter {

	private Context 					context;//运行上下文
	private List<Behavior> 				listItems;//数据集合
	private LayoutInflater 				listContainer;//视图容器
	private int 						itemViewResource;//自定义项视图源
	private BitmapManager 				bmpManager;
	
	static class ListItemView{				//自定义控件集合  
			public ImageView userface;  
		    public TextView content;
		    public ImageView book_image1;
		    public ImageView book_image2;
		    public ImageView book_image3;
		    public TextView client;
		    public TextView date; 
	 }  

	/**
	 * 实例化Adapter
	 * @param context
	 * @param data
	 * @param resource
	 */
	public ListViewMyTrendsAdapter(Context context, List<Behavior> data, int resource ) {
		this.context = context;			
		this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
		this.bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_dface_loading));
	}
	
	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}
	   
	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {

		//自定义视图
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			//获取list_item布局文件的视图
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();
			//获取控件对象
			
			listItemView.userface 		= (ImageView)convertView.findViewById(R.id.recent_trends_listitem_userface);
			listItemView.content 		= (TextView)convertView.findViewById(R.id.recent_trends_listitem_content);
			listItemView.book_image1 	= (ImageView)convertView.findViewById(R.id.recent_trends_listitem_image1);
			listItemView.book_image2 	= (ImageView)convertView.findViewById(R.id.recent_trends_listitem_image2);
			listItemView.book_image3 	= (ImageView)convertView.findViewById(R.id.recent_trends_listitem_image3);
			
			listItemView.date= (TextView)convertView.findViewById(R.id.recent_trends_listitem_date);
			listItemView.client= (TextView)convertView.findViewById(R.id.recent_trends_listitem_client);
			
			//设置控件集到convertView
			convertView.setTag(listItemView);
		}else {
			listItemView = (ListItemView)convertView.getTag();
		}
				
		//设置文字和图片
		Behavior behavior = listItems.get(position);
		
		String faceURL = behavior.getUserAvatar();
		if(faceURL.endsWith("portrait.gif") || StringUtils.isEmpty(faceURL)){
			listItemView.userface.setImageResource(R.drawable.widget_dface);
		}else{
			bmpManager.loadBitmap(faceURL, listItemView.userface);
		}
		listItemView.userface.setOnClickListener(faceClickListener);
		listItemView.userface.setTag(behavior);
				
		int action_type = behavior.getActionType();		
		if( action_type == 0 )
		{
			
			List<divText> my_textList = new ArrayList<divText>(); 
			divText div_text1 = new divText();
			div_text1.text = behavior.getUserName();
			div_text1.is_mark = true;
			my_textList.add(div_text1);
			
			divText div_text2 = new divText();
			div_text2.text = behavior.getAction();
			div_text2.is_mark = false;
			my_textList.add(div_text2);
			
			divText div_text3 = new divText();
			div_text3.text = behavior.getTargetTitle();
			div_text3.is_mark = true;
			my_textList.add(div_text3);
			
			CustomTextView.setClickableTextView(context, listItemView.content, my_textList);
					
			listItemView.content.setTag(behavior);//设置隐藏参数(实体类)
			listItemView.content.setOnClickListener(linkViewClickListener);
			
			listItemView.book_image1.setVisibility(ImageView.GONE);
			listItemView.book_image2.setVisibility(ImageView.GONE);
			listItemView.book_image3.setVisibility(ImageView.GONE);
		}
		
		if( action_type == 1 || action_type == 4 || action_type == 5 )
		{
			
			List<divText> my_textList = new ArrayList<divText>(); 
			divText div_text1 = new divText();
			div_text1.text = behavior.getUserName();
			div_text1.is_mark = true;
			my_textList.add(div_text1);
			
			divText div_text2 = new divText();
			div_text2.text = behavior.getAction();
			div_text2.is_mark = false;
			my_textList.add(div_text2);
			
			divText div_text3 = new divText();
			div_text3.text = behavior.getTargetTitle();
			div_text3.is_mark = true;
			my_textList.add(div_text3);

			CustomTextView.setClickableTextView(context, listItemView.content, my_textList);
			
			listItemView.content.setTag(behavior);//设置隐藏参数(实体类)
			listItemView.content.setOnClickListener(linkViewClickListener);			
			
			List<BooklistItem> booklist_items = behavior.getBooklistItems();
			if( !booklist_items.isEmpty() ) {
				
				int len = booklist_items.size();
				
				if( len >= 3 ) {
				
					BooklistItem booklist_item = booklist_items.get(0);
					String book_image_url = booklist_item.getBookImageUrl();
					
					if(StringUtils.isEmpty(book_image_url)){
						listItemView.book_image1.setVisibility(ImageView.GONE);
					}else{
						bmpManager.loadBitmap(book_image_url, listItemView.book_image1, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
						listItemView.book_image1.setOnClickListener(imageClickListener);

						listItemView.book_image1.setTag( book_image_url );
						listItemView.book_image1.setVisibility(ImageView.VISIBLE);;
					}
									
					booklist_item = booklist_items.get(1);
					book_image_url = booklist_item.getBookImageUrl();
					
					if(StringUtils.isEmpty(book_image_url)){
						listItemView.book_image2.setVisibility(ImageView.GONE);
					}else{
						bmpManager.loadBitmap(book_image_url, listItemView.book_image2, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
						listItemView.book_image2.setOnClickListener(imageClickListener);

						listItemView.book_image2.setTag(book_image_url);
						listItemView.book_image2.setVisibility(ImageView.VISIBLE);;
					}
				
					booklist_item = booklist_items.get(2);
					book_image_url = booklist_item.getBookImageUrl();
										
					if(StringUtils.isEmpty(book_image_url)){
						listItemView.book_image3.setVisibility(ImageView.GONE);
					}else{
						bmpManager.loadBitmap(book_image_url, listItemView.book_image3, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
						listItemView.book_image3.setOnClickListener(imageClickListener);

						listItemView.book_image3.setTag(book_image_url);
						listItemView.book_image3.setVisibility(ImageView.VISIBLE);;
					}
				}
				else
				{
					if( len == 2 )
					{
						BooklistItem booklist_item = booklist_items.get(0);
						String book_image_url = booklist_item.getBookImageUrl();
						
						if(StringUtils.isEmpty(book_image_url)){
							listItemView.book_image1.setVisibility(ImageView.GONE);
						}else{
							bmpManager.loadBitmap(book_image_url, listItemView.book_image1, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
							listItemView.book_image1.setOnClickListener(imageClickListener);

							listItemView.book_image1.setTag( book_image_url );
							listItemView.book_image1.setVisibility(ImageView.VISIBLE);;
						}
					
						booklist_item = booklist_items.get(1);
						book_image_url = booklist_item.getBookImageUrl();
						
						if(StringUtils.isEmpty(book_image_url)){
							listItemView.book_image2.setVisibility(ImageView.GONE);
						}else{
							bmpManager.loadBitmap(book_image_url, listItemView.book_image2, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
							listItemView.book_image2.setOnClickListener(imageClickListener);

							listItemView.book_image2.setTag( book_image_url );
							listItemView.book_image2.setVisibility(ImageView.VISIBLE);;
						}
						
						listItemView.book_image3.setVisibility(ImageView.GONE);
				
					}
					
					if( len == 1)
					{
						BooklistItem booklist_item = booklist_items.get(0);
						String book_image_url = booklist_item.getBookImageUrl();
						
						if(StringUtils.isEmpty(book_image_url)){
							listItemView.book_image1.setVisibility(ImageView.GONE);
						}else{
							bmpManager.loadBitmap(book_image_url, listItemView.book_image1, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
							listItemView.book_image1.setOnClickListener(imageClickListener);

							listItemView.book_image1.setTag( book_image_url );
							listItemView.book_image1.setVisibility(ImageView.VISIBLE);;
						}
						
						listItemView.book_image2.setVisibility(ImageView.GONE);
						listItemView.book_image3.setVisibility(ImageView.GONE);
					}
				}
			} else {

				listItemView.book_image1.setVisibility(ImageView.GONE);
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);

			}
		}
		
		if( action_type == 2 || action_type == 3  )
		{
			List<divText> my_textList = new ArrayList<divText>(); 
			
			divText div_text1 = new divText();
			div_text1.text = behavior.getUserName();
			div_text1.is_mark = true;
			my_textList.add(div_text1);
			
			divText div_text2 = new divText();
			if( action_type == 2)
				div_text2.text = "在你创建的书单";
			if( action_type == 3)
				div_text2.text = "在你关注的书单";			
			div_text2.is_mark = false;
			my_textList.add(div_text2);
			
			divText div_text3 = new divText();
			div_text3.text = behavior.getTargetPtitle();
			div_text3.is_mark = true;
			my_textList.add(div_text3);
			
			divText div_text4 = new divText();
			div_text4.text = behavior.getAction();
			div_text4.is_mark = false;
			my_textList.add(div_text4);
			
			divText div_text5 = new divText();
			div_text5.text = "《" + behavior.getTargetTitle()  + "》";
			div_text5.is_mark = true;
			my_textList.add(div_text5);
			CustomTextView.setClickableTextView(context, listItemView.content, my_textList);	

			listItemView.content.setTag(behavior);//设置隐藏参数(实体类)
			listItemView.content.setOnClickListener(linkViewClickListener);
			
			String book_image_url = behavior.getTargetImgUrl();
			if(!StringUtils.isEmpty(book_image_url)) {
				
				bmpManager.loadBitmap(book_image_url, listItemView.book_image1, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
				listItemView.book_image1.setOnClickListener(imageClickListener);

				listItemView.book_image1.setTag( book_image_url );
				listItemView.book_image1.setVisibility(ImageView.VISIBLE);
				
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);
				
			}else{
				
				listItemView.book_image1.setVisibility(ImageView.GONE);
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);
				
			}
		}
		
		if( action_type == 6 || action_type == 7  )
		{

			List<divText> my_textList = new ArrayList<divText>(); 
			divText div_text1 = new divText();
			div_text1.text = behavior.getUserName();
			div_text1.is_mark = true;
			my_textList.add(div_text1);
			
			divText div_text2 = new divText();
			div_text2.text = "在书单";
			div_text2.is_mark = false;
			my_textList.add(div_text2);
			
			divText div_text3 = new divText();
			div_text3.text = behavior.getTargetPtitle();
			div_text3.is_mark = true;
			my_textList.add(div_text3);
			
			divText div_text4 = new divText();
			div_text4.text = behavior.getAction();
			div_text4.is_mark = false;
			my_textList.add(div_text4);
			
			divText div_text5 = new divText();
			div_text5.text = "《" + behavior.getTargetTitle()  + "》";
			div_text5.is_mark = true;
			my_textList.add(div_text5);
			CustomTextView.setClickableTextView(context, listItemView.content, my_textList);	

			listItemView.content.setTag(behavior);//设置隐藏参数(实体类)
			listItemView.content.setOnClickListener(linkViewClickListener);

			String book_image_url = behavior.getTargetImgUrl();
			if(!StringUtils.isEmpty(book_image_url)) {
				bmpManager.loadBitmap(book_image_url, listItemView.book_image1, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
				listItemView.book_image1.setOnClickListener(imageClickListener);

				listItemView.book_image1.setTag( book_image_url );
				
				listItemView.book_image1.setVisibility(ImageView.VISIBLE);
				
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);
			}else{
				listItemView.book_image1.setVisibility(ImageView.GONE);
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);
			}
			
		}
		
		if( action_type == 12 )
		{		
			List<divText> my_textList = new ArrayList<divText>(); 
			divText div_text1 = new divText();
			div_text1.text = behavior.getUserName();
			div_text1.is_mark = true;
			my_textList.add(div_text1);
			
			divText div_text2 = new divText();
			div_text2.text = behavior.getAction();
			div_text2.is_mark = false;
			my_textList.add(div_text2);
			
			divText div_text3 = new divText();
			div_text3.text = "《" + behavior.getTargetTitle() + "》";
			div_text3.is_mark = true;
			my_textList.add(div_text3);
			CustomTextView.setClickableTextView(context, listItemView.content, my_textList);
			
			listItemView.content.setTag(behavior);//设置隐藏参数(实体类)
			listItemView.content.setOnClickListener(linkViewClickListener);
			
			String book_image_url = behavior.getTargetImgUrl();
			if(!StringUtils.isEmpty(book_image_url)) {
				bmpManager.loadBitmap(book_image_url, listItemView.book_image1, BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
				listItemView.book_image1.setOnClickListener(imageClickListener);

				listItemView.book_image1.setTag(book_image_url);
				listItemView.book_image1.setVisibility(ImageView.VISIBLE);
				
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);
			}else{
				listItemView.book_image1.setVisibility(ImageView.GONE);
				listItemView.book_image2.setVisibility(ImageView.GONE);
				listItemView.book_image3.setVisibility(ImageView.GONE);
			}
			
		}
		
		switch(behavior.getAppClient())
		{
			default:
				listItemView.client.setText("");
				break;
					
			case Behavior.CLIENT_MOBILE:
				listItemView.client.setText("来自:手机");
				break;
			case Behavior.CLIENT_ANDROID:
				listItemView.client.setText("来自:Android");
				break;
			case Behavior.CLIENT_IPHONE:
				listItemView.client.setText("来自:iPhone");
				break;
			case Behavior.CLIENT_WINDOWS_PHONE:
				listItemView.client.setText("来自:Windows Phone");
				break;
			
		}
		
		if(StringUtils.isEmpty(listItemView.client.getText().toString()))
			listItemView.client.setVisibility(View.GONE);
		else
			listItemView.client.setVisibility(View.VISIBLE);
		
		listItemView.date.setText(StringUtils.friendly_time(behavior.getTime()));
			
		return convertView;
	}
	
	private View.OnClickListener faceClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			Behavior behavior = (Behavior)v.getTag();
			UIHelper.showUserCenter(v.getContext(), behavior.getUserId(), behavior.getUserName());
		}
	};
	
	private View.OnClickListener imageClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			UIHelper.showImageDialog(v.getContext(), (String)v.getTag());			
		}
	};
	
	private View.OnClickListener linkViewClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			if(!isLinkViewClick()) {
				Behavior behavior = (Behavior)v.getTag();
				
				int action_type = behavior.getActionType();
				
				if( action_type == 1 || action_type == 4 || action_type == 5 ) {
					int booklist_id = behavior.getTargetId();
					UIHelper.showBooklistDetail(v.getContext(), booklist_id );
				}
				
				if( action_type == 2 || action_type == 3 || action_type == 6 || action_type == 7 ) {
					int booklist_id = behavior.getTargetPid();
					UIHelper.showBooklistDetail(v.getContext(), booklist_id );
				}
								
				if( action_type == 12 ) {
					int book_id = behavior.getTargetId();
					UIHelper.showBookDetail(v.getContext(), book_id);
				}
			}
			setLinkViewClick(false);
		}
	};
	
	private OnLinkClickListener linkClickListener = new OnLinkClickListener() {
		public void onLinkClick() {
			setLinkViewClick(true);
		}
	};

}
